// pages/sell/sell.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow:false,
    jrjyl:'',//今日交易量
    zxql:'',//总需求量
    integral:'',//当前积分价格
    fee:0.5,//手续费
    num:'',//买出数量
    price:'',//买出单价
    sum:'',//买出总价
    password:''
  },
  toOrder(){
    wx.navigateTo({
      url: '/pages/order/order',
    })
  },
  // 判断用户有没有买入/卖出记录
  order(){
    var that=this
    wx.request({
      url: app.globalData.baseUrl + '/integral/getIntegralTranCount',
      data:{
        xcxOpenId: app.globalData.xcxOpenId,
       },
       success(res){
         console.log(res)
         if(res.data.code!=0){
           that.setData({isShow:true})
         }
       }
    })
  },
    /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.order()
     // 对手续费做截取小数点后两位操作
    var fee = this.data.fee.toFixed(2)
    this.setData({fee})
    var that = this
    // 获取顶部数据
    wx.request({
      url: app.globalData.baseUrl + '/integral/findTopData',
      data:{
       xcxOpenId: app.globalData.xcxOpenId,
      },
      success(res){
        const data = res.data.data
        that.setData({
          jrjyl: data.jrjyl,
          zxql: data.zxql
        })
        if(data.integral === undefined){
          that.setData({integral: 0})
        }else{
          that.setData({
            integral: data.integral
          })
        }
      }
    })
  },

  // 显示支付密码输入层
  showInputLayer: function(){
    this.setData({
      showPayPwdInput: true,
      payFocus: true});
  },
  // 隐藏支付密码输入层
  hidePayLayer: function(){
    this.setData({ 
      showPayPwdInput: false,
      payFocus: false,
      pwdVal: '',
      password: this.data.pwdVal 
    });
  },
  // 获取焦点
  getFocus: function(){
    this.setData({ payFocus: true });
  },
  // 输入密码监听
  inputPwd: function(e){
      this.setData({ pwdVal: e.detail.value });
      if (e.detail.value.length >= 6){
        this.hidePayLayer();  
        var that = this        
        wx.request({
          url: app.globalData.baseUrl + '/integral/buyIn',     
          data:{
            xcxOpenId: app.globalData.xcxOpenId,
            price: that.data.price,
            num: that.data.num,
            handFree: that.data.price*that.data.num*0.5,
            total: that.data.sum,
            password: that.data.password
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          method:'POST',
          success(res){
            wx.showToast({
              title: res.data.msg,
            })
          }
        })
      }
  },
// 、、、、、

  //获取input中买入数量
  getNum(e){
    this.setData({num:e.detail.value})
     // 计算总价
     let sum = this.data.sum
     sum = parseInt(this.data.num) * parseInt(this.data.price)
    //  如果买入数量跟买入单价都有数据则将算出的总价赋给买入总价sum，若两个中有一个没有数据则将买入总价设置为不显示任何内容（此步骤是为了解决页面会显示null的问题）
     if(sum){
        this.setData({sum})
      }else{
        this.setData({sum: ''})
      }
  },
  //获取input中买入单价
  getPrice(e){
    this.setData({price: e.detail.value})
    // 计算总价
    let sum = this.data.sum
    sum = parseInt(this.data.num) * parseInt(this.data.price)
    if(sum){
      this.setData({sum})
    }else{
      this.setData({sum:''})
    }
     
  },
  // 点击买入按钮提交数据
  Buy(){
    this.showInputLayer();
  }
})